package com.wanshi.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanshi.entity.Goods;
import com.wanshi.entity.Supplier;
import com.wanshi.mapper.GoodsMapper;
import com.wanshi.service.IGoodsService;
import com.wanshi.service.ISupplierService;
import com.wanshi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-16
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ISupplierService supplierService;


    @Override
    public List<Goods> findAllBySupId(String supplier_id) {

        return goodsMapper.findAllBySupId(supplier_id);
    }

    @Override
    public IPage findPageGoods(IPage page, Goods goods) {
        return goodsMapper.findPageGoods(page, goods);
    }

    @Override
    public Result findById(int id) {
        Goods goods = baseMapper.selectById(id);
        if(goods!=null && goods.getSupplierId() !=null){
            Supplier supplier = supplierService.getById(goods.getSupplierId());
            if(supplier!=null){
                goods.setSupplierName(supplier.getName());
            }
        }
        return Result.ok(goods);
    }
}
